﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NSsh.Server.Services;
using System.Security.Principal;
using log4net;

namespace NSsh.Server.ChannelLayer.Console
{
    public class CommandConsole : BaseProcessConsole
    {
        public CommandConsole(IImpersonationProvider impersonationProvider, IIdentity identity, string password)
            : base(impersonationProvider, identity, password, FindCmd(), string.Empty, Path.GetDirectoryName(FindCmd()))
        {
        }

        public CommandConsole(IImpersonationProvider impersonationProvider, IIdentity identity, string password, string command)
            : base(impersonationProvider, identity, password, FindCmd(), "/c " + command, Path.GetDirectoryName(FindCmd()))
        {
        }

        public static string FindCmd()
        {
            string[] cmdLocations = new string[]
            {
                Path.Combine(Environment.GetEnvironmentVariable("windir"), @"system32\cmd.exe"),
                Path.Combine(Environment.GetEnvironmentVariable("windir"), @"Syswow64\cmd.exe")
            };

            foreach (string cmdLocation in cmdLocations)
            {
                if (File.Exists(cmdLocation)) return cmdLocation;
            }

            return null;
        }
    }
}
